

#include <QtGui>

#include "glwidget.h"
#include "window.h"

#include "DbConnection.h"
#include "controlpanel.h"
#include <iostream>
#include <sstream>
#include "src/db/DbModule.h"
#include "src/net/RoppenServer.h"
#include "src/net/RoppenListener.h"
#include "include/dto/camData.h"
#include "include/dto/3d/matrix.h"

Window::Window()
{




	std::list<Renderable*> *list = new std::list<Renderable*>;
	DataPool *pool = new DataPool;
	DbImport::importModule(pool);
	CamDataImport::importModels(pool);
	MatrixDataImport::importModels(pool);

//	DbConfig *conf = new DbConfig(pool,MODULE_DB,"192.168.1.101","vidipedi","postgres","nugise71");
	DbConfig *conf = new DbConfig(pool,MODULE_DB,"127.0.0.1","vidipedi","postgres","nugise71");

	pool->addRootModel(conf);
	Db *db = (Db*)pool->createModule(MODULE_DB);

	pool->setIdProvider(db);

	manager.setPool(pool);
	manager.registerBehaviour(&keySelector);

	VideoDisplay *display = new VideoDisplay(pool,"display","videoDisplay");
	pool->addRootModel(display);
	list->push_back(display);

	glWidget = new GLWidget;




	glWidget->setRenderList(list);


//	XYZFloat *t = display->pos->get();
//
//	FloatSlotConnector *x = new FloatSlotConnector(t->x);
//	FloatSlotConnector *y = new FloatSlotConnector(t->y);
//	FloatSlotConnector *z = new FloatSlotConnector(t->z);
//
//	connect(xSlider, SIGNAL(valueChanged(int)), x, SLOT(set(int)));
//	connect(ySlider, SIGNAL(valueChanged(int)), y, SLOT(set(int)));
//	connect(zSlider, SIGNAL(valueChanged(int)), z, SLOT(set(int)));
	//connect(xSlider, SIGNAL(valueChanged(int)), glWidget, SLOT(setXRotation(int)));
	//connect(glWidget, SIGNAL(xRotationChanged(int)), xSlider, SLOT(setValue(int)));
	//connect(ySlider, SIGNAL(valueChanged(int)), glWidget, SLOT(setYRotation(int)));
	//connect(glWidget, SIGNAL(yRotationChanged(int)), ySlider, SLOT(setValue(int)));
	//connect(zSlider, SIGNAL(valueChanged(int)), glWidget, SLOT(setZRotation(int)));
	//connect(glWidget, SIGNAL(zRotationChanged(int)), zSlider, SLOT(setValue(int)));

	QVBoxLayout *mainLayout = new QVBoxLayout;
	mainLayout->addWidget(glWidget);

	//Alpbach
	setAutoFillBackground(true);
	QPalette palette;
	palette.setBrush(backgroundRole(),QColor::fromRgb(0,0,0));
	setPalette(palette);

	if (!IsControl::control()) {
		RoppenServer *s = new RoppenServer(display->preview->get());
		const QPalette p = QPalette(QPalette::Shadow);
		setPalette(p);
		setAutoFillBackground(true);
		RoppenTextServer *s2 = new RoppenTextServer(glWidget);
	}
	else {
		RoppenTextServer *s2 = new RoppenTextServer(glWidget);
		PingServer *ping = new PingServer();
		//Alpbach
//		QTcpSocket *tcpSocket = new QTcpSocket(this);
//		CountAndSend *send = new CountAndSend(tcpSocket);
//		tcpSocket->connectToHost("192.168.1.103",6667);
//		NetIntListener *r = new NetIntListener(10,send);
//
//		DisplayPanel *p = display->preview->get();
//		p->rot->get()->x->addConsumer(r);
//		p->rot->get()->y->addConsumer(r);
//		p->rot->get()->z->addConsumer(r);
//
//		NetIntListener *r1 = new NetIntListener(11,send);
//		p->pos->get()->x->addConsumer(r1);
//		p->pos->get()->y->addConsumer(r1);
//		p->pos->get()->z->addConsumer(r1);
//
//		NetIntListener *r2 = new NetIntListener(12,send);
//		p->text->get()->length->addConsumer(r2);
//
//		p->text->get()->stop->addConsumer(r2);
//		p->text->get()->start->addConsumer(r2);
//		p->text->get()->playMode->addConsumer(r2);
//		p->text->get()->startStop->addConsumer(r2);
//
//		p->text->get()->fps->addConsumer(r2);
//		p->text->get()->camToTime->addConsumer(r2);
//		p->text->get()->opacity->addConsumer(r2);
//		p->text->get()->current->addConsumer(r2);
//		p->text->get()->frameIncrement->addConsumer(r2);
//		p->text->get()->filebase->addConsumer(r2);
//		p->text->get()->filename->addConsumer(r2);
//
//
//		ControlPanel *cp = new ControlPanel(this);
//		cp->setDisplay(display);
//		cp->setPool(pool);
//		cp->setGl(glWidget);
//		mainLayout->addWidget(cp);


	}
	setLayout(mainLayout);

//	xSlider->setValue(0 * 16);
//	ySlider->setValue(0 * 16);
//	zSlider->setValue(0 * 16);
	setWindowTitle(tr("plankton Labs - nemosomen - videpedi"));

	//TODO replace
	if (IsControl::control())
		showFullScreen();

	MidiReader *m = new MidiReader("/dev/midi2");
//	m->setHandler(new MatrixMidiHandler(display));
	m->setHandler(new AlpbachMidiHandler(display));
	m->start();

}

void Window::keyPressEvent(QKeyEvent *event )  {
//	printf("Key: %d\n",event->key());
//	printf("Key: %d\n",event->nativeModifiers());
	int modifiers = event->nativeModifiers();
	bool added = false;
	std::ostringstream os;
	int k = event->key();
	if (k>1000)
		return;

	if ((modifiers|4)==modifiers) {
		os << "C";
		added=true;
	}
	if ((modifiers|8)==modifiers) {
		os << "M";
		added=true;
	}
	if ((modifiers|1)==modifiers) {
		os << "S";
		added=true;
	}
	if (added) {
		os<<"-";
	}
	os<< (char)k;
	std::string key = os.str();

	printf(key.data());
	printf("\n");



	manager.execute(&keySelector,key);




}

